package com.dhsc.service.impl.xtqxgl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.xtqxgl.TsglVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.xtqxgl.ITsglDao;
import com.dhsc.service.xtqxgl.ITsglService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: TsglServiceImpl
 * @Description: 用户特殊管理
 * @date: 2023/11/6 14:36
 * @author: gx
 */
@Service
@Slf4j
public class TsglServiceImpl implements ITsglService {

    @Autowired
    private ITsglDao tsglDao;

    @Override
    public LayuiTableVo queryTsgl(TsglVo tsglVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(tsglVo.getPage(), tsglVo.getLimit());
            List<TsglVo> list = tsglDao.queryTsgl(tsglVo);
            PageInfo<TsglVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询用户特殊管理信息出错",e);
            throw new MyException("查询用户特殊管理信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveTsgl(TsglVo tsglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            tsglVo.setJhbz("1");
            List<TsglVo> tsglVos = tsglDao.queryTsgl(tsglVo);
            if (CommonUtil.isNotEmptyList(tsglVos)){
                obj.setStatus(202);
                obj.setMessage("该机构代码与用户已存在，且已激活");
                return obj;
            }
            tsglDao.insertTsgl(tsglVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存用户特殊管理信息出错", e);
            throw new MyException("保存用户特殊管理信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteTsgl(TsglVo tsglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            tsglDao.updateTsgl(tsglVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除用户特殊管理信息出错", e);
            throw new MyException("删除用户特殊管理信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryTsryxm(TsglVo tsglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            List<TsglVo> list= tsglDao.queryTsryxm(tsglVo);
            obj.setData(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询特殊人员姓名信息出错", e);
            throw new MyException("查询特殊人员姓名信息出错",e);
        }
        return obj;
    }
}
